import { getNewValue } from ".";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheetName, row, table, store, old, sheet, insertRowCount,insertType} = options;
        context.suspendPaint();
        try {
            const rowHeight = {};
            const bindingPath = table.bindingPath();
            if (isUndo) {
                store.dataSourceInstance.setValue(bindingPath, old.tableValue);
                table.bindingPath(bindingPath);
                for(let key in options._oldRowHeight) {
                    sheet.setRowHeight(key, options._oldRowHeight[key]);
                }
            } else {
                const tableValue = store.dataSourceInstance.getValue(bindingPath);
                options.old = { tableValue: [...tableValue] };
                const { row: startRow, rowCount: tableRowCount } = table.dataRange();
                options._oldRowHeight = {};
                for(let r = startRow; r < tableRowCount + startRow; r++) {
                    options._oldRowHeight[r] = sheet.getRowHeight(r);
                }
                if(row<startRow){  
                    rowHeight[row] = sheet.getRowHeight(row,window.GC.Spread.Sheets.SheetArea.viewport);
                }
                tableValue.forEach((value,index)=>{
                    const height = sheet.getRowHeight(index+startRow,window.GC.Spread.Sheets.SheetArea.viewport);
                    rowHeight[index+startRow]=height;
                })
                const rowNum = insertType==='top' ? row-1 : insertType==='bottom' ? row+insertRowCount-1 : row;
                const rowIndex = rowNum - startRow;
                const newValue = row<startRow?getNewValue(tableValue, insertRowCount-2, [],insertRowCount):getNewValue(tableValue, rowIndex, [],insertRowCount);
                store.dataSourceInstance.setValue(bindingPath, newValue);

                table.bindingPath(bindingPath);
                const newTableValue = store.dataSourceInstance.getValue(bindingPath);
                const { col,colCount } = table.dataRange();
                newTableValue.forEach((value,index) => {
                    if(row<startRow){
                        for(var i=0;i<insertRowCount;i++){
                            sheet.setRowHeight(index+row+1, rowHeight[index+row-i], window.GC.Spread.Sheets.SheetArea.viewport);
                        }
                    }else if (index+startRow>row+insertRowCount-1){ 
                        for(var i=0;i<insertRowCount;i++){
                            sheet.setRowHeight(index+startRow, rowHeight[index+startRow-1-i], window.GC.Spread.Sheets.SheetArea.viewport);
                                for(var j=0;j<colCount;j++){
                                    const cstyle = sheet.getStyle(index+startRow-1-i,col+j,window.GC.Spread.Sheets.SheetArea.viewport, true);
                                    if(cstyle) {
                                        cstyle.cellType = null;
                                        sheet.setStyle(index+startRow,col+j,cstyle,window.GC.Spread.Sheets.SheetArea.viewport);
                                    }
                                }
                        }
                    }
                });

            }
            
			//table.bindingPath(bindingPath);
            
            // const tableValue = store.dataSourceInstance.getValue(bindingPath);
            // const { row: startRow,col,colCount } = table.dataRange();
            // tableValue.forEach((value,index) => {
            //     if(row<startRow){
            //         for(var i=0;i<insertRowCount;i++){
            //             sheet.setRowHeight(index+row+1, rowHeight[index+row-i], GC.Spread.Sheets.SheetArea.viewport);
            //         }
            //     }else if (index+startRow>row+insertRowCount-1){ 
            //         for(var i=0;i<insertRowCount;i++){
            //             sheet.setRowHeight(index+startRow, rowHeight[index+startRow-1-i], GC.Spread.Sheets.SheetArea.viewport);
            //                 for(var j=0;j<colCount;j++){
            //                     const cstyle = sheet.getStyle(index+startRow-1-i,col+j,GC.Spread.Sheets.SheetArea.viewport, true);
            //                     if(cstyle) {
            //                         cstyle.cellType = null;
            //                         sheet.setStyle(index+startRow,col+j,cstyle,GC.Spread.Sheets.SheetArea.viewport);
            //                     }
            //                 }
            //         }
            //     }
            // });

            store.calcAll(true, sheetName);
        } catch (e) {
            if(!!(window.console&&window.console.log)) {
                window.console.log(e);
            }
        }
    
        context.resumePaint();
  

    }
};